package com.lancoo.examview.weike;

import static android.view.View.OVER_SCROLL_NEVER;

import android.annotation.SuppressLint;
import android.os.Build;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewParent;
import android.webkit.WebChromeClient;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.FrameLayout;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;

import com.lancoo.answer.util.DpUtils;
import com.lancoo.examview.R;
import com.rxjava.rxlife.RxLife;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;

import java.util.concurrent.Callable;

import io.reactivex.Single;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.schedulers.Schedulers;

/**
 * Created by pengmi on 2022/7/25 11:54
 * Description:
 */
public class TopicBodyActivity extends AppCompatActivity {

    private String content="<html>  \n" +
            "      \n" +
            "        <head>  \n" +
            "      \n" +
            "            <meta http-equiv=\"Content-Type\" content=\"text/html; charset=UTF-8\">  \n" +
            "          \n" +
            "            <title>Insert title here</title>  \n" +
            "      \n" +
            "        </head>  \n" +
            "      \n" +
            "        <body>  \n" +
            "      \n" +
            "            <a href=\"m://com.lancoo.examview/?text=android\">打开app</a><br/>  \n" +
            "      \n" +
            "        </body>  \n" +
            "      \n" +
            "    </html>  ";
    private WebView webView;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_topic_body);
        loadTopicContentInVebView(content);


    }


    private void loadTopicContentInVebView(String content){
        webView=new WebView(getApplicationContext());
        FrameLayout view = findViewById(R.id.frameLayout);
        view.addView(webView);
        setWebViewSettings();
        String url = "file:///android_asset/test.html";
        webView.loadUrl(url);
    }


    @Override
    protected void onDestroy() {
        super.onDestroy();
        if(webView!=null){
            ViewParent parent = webView.getParent();
            if(parent instanceof ViewGroup){
                ((ViewGroup) parent).removeView(webView);
            }
            webView.stopLoading();
            webView.getSettings().setJavaScriptEnabled(false);
            webView.clearHistory();
            webView.removeAllViews();
            webView.destroy();
            webView = null;
        }
    }


    /**
     * 设置webViewSetting
     */
    @SuppressLint("SetJavaScriptEnabled")
    private void setWebViewSettings(){
        webView.setHorizontalFadingEdgeEnabled(false);
        webView.setVerticalScrollBarEnabled(false);
        webView.setOverScrollMode(OVER_SCROLL_NEVER);
        webView.setScrollBarSize(0);
        WebSettings webSettings = webView.getSettings();
        webSettings.setJavaScriptEnabled(true);
        webSettings.setJavaScriptCanOpenWindowsAutomatically(true);
        webSettings.setLayoutAlgorithm(WebSettings.LayoutAlgorithm.SINGLE_COLUMN);
        webSettings.setTextZoom(100);
        webSettings.setAllowUniversalAccessFromFileURLs(true);
        webSettings.setDomStorageEnabled(true);//开启本地DOM存储
        webSettings.setAppCacheEnabled(true);
        webSettings.setLoadWithOverviewMode(true);
        webView.setDrawingCacheEnabled(false);
        webView.setNestedScrollingEnabled(true);
        webView.setWebChromeClient(new WebChromeClient());
        webView.setWebViewClient(new WebViewClient(){
            //重写各种回调...

            @Override
            public void onPageFinished(WebView view1, String url) {
                super.onPageFinished(view1, url);

            }
            @Override
            public boolean shouldOverrideUrlLoading(WebView view, String  url) {
                //如果对不同的地址需要做不同的处理的话//if(url.contains("")){//}
                return true;
            }
        });
    }

}
